﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Xml.Linq;
using System.Threading;
using System.Windows.Threading;

namespace charlieAngel
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string m_RSS = @"http://e.wtx.me/charliesangels/sign/ssn.asp";

        public MainWindow()
        {
            InitializeComponent();

            BitmapImage promo = FetchXML();

            IMG_Wishoo.Source = promo;

            DispatcherTimer dpT = new DispatcherTimer();
            dpT.Tick += new EventHandler(dpT_Tick);
            dpT.Interval = new TimeSpan(0, 0, 25);
            dpT.Start();
        }

        void dpT_Tick(object sender, EventArgs e)
        {
            BitmapImage promo = FetchXML();

            Thread.Sleep(5000);

            IMG_Wishoo.Source = promo;
        }

        protected BitmapImage FetchXML()
        {
            string returnString="";
            XDocument xWishoo = XDocument.Load(m_RSS);
            IEnumerable<XElement> feedElements = xWishoo.Descendants("SSDATA");

            foreach (XElement el in feedElements)
            {
                XElement imageURL = el.Element("URL");
                returnString = imageURL.Value;
            }


            BitmapImage promo = new BitmapImage();
            promo.BeginInit();
            promo.UriSource = new Uri(returnString);
            promo.EndInit();

            return promo;
        }
    }
}
